package com.hd.hdxgddcommon.utils;

public class NumberUtil {
    private final static char[] HEX = "0123456789abcdef".toCharArray();

    public static String toFullHexString(byte num) {
//        return Integer.toHexString(num & 0xff);
        char[] chs = new char[2];
        for (int i = 0; i < chs.length; i++) {
            chs[chs.length - 1 - i] = HEX[(num >> (i * 4)) & 0xf];
        }
        return new String(chs);
    }

    public static boolean isNeg(byte b) {
        int i = (b & 0xff) >> 7;
        return i == 1;
    }

    public static String bcd2str(byte[] bcd) {
        if (null == bcd || bcd.length == 0) {
            return "";
        } else {
            // 存储转码后的字符串
            StringBuilder sb = new StringBuilder();

            // 循环数组解码
            for (int i = 0; i < bcd.length; i++) {
                // 转换低字节
                int low = (bcd[i] & 0x0f);
                sb.append(low);

                // 转换高字节
                int high = ((bcd[i] & 0xf0) >> 4);

                // 如果高字节等于0xf说明是补的字节，直接抛掉
                if (high != 0xf) {
                    sb.append(high);
                }
            }

            // 返回解码字符串
            return sb.toString();
        }
    }

    public double log(double value, double base) {
        return Math.log(value) / Math.log(base);
    }
}
